package GUI;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import ctrLayer.ForestOwnerCtr;

/**
 * CreateOwnerFrame
 * @author Thomas
 */
public class CreateOwnerFrame extends JFrame {
	
	private ForestOwnerCtr oCtr;
	private OwnerPanel ownerPanel;
	
	private JTextField txtOwnerName;
	private JTextField txtOwnerAddress;
	private JTextField txtOwnerPhonenumber;

	public CreateOwnerFrame() {
		oCtr = ForestOwnerCtr.getInstance();
		initilizeFrame();
		ownerPanel = OwnerPanel.getInstance();
	}

	private void initilizeFrame() {
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		
		setTitle("Create Owner");
		setBounds((int) d.getWidth()/4, (int) d.getHeight() / 5, 364, 157);
		setResizable(false);
		getContentPane().setLayout(null);
		
		JLabel lblForestName = new JLabel("Name");
		lblForestName.setBounds(10, 15, 120, 20);
		getContentPane().add(lblForestName);
		
		txtOwnerName = new JTextField();
		txtOwnerName.setBounds(140, 15, 200, 20);
		getContentPane().add(txtOwnerName);
		txtOwnerName.setColumns(10);
		
		JLabel lblOwnerAddress = new JLabel("Address");
		lblOwnerAddress.setBounds(10, 40, 120, 20);
		getContentPane().add(lblOwnerAddress);
		
		txtOwnerAddress = new JTextField();
		txtOwnerAddress.setBounds(140, 40, 200, 20);
		getContentPane().add(txtOwnerAddress);
		txtOwnerAddress.setColumns(10);
		
		JLabel lblOwnerPhonenumber = new JLabel("Phonenumber");
		lblOwnerPhonenumber.setBounds(10, 65, 132, 20);
		getContentPane().add(lblOwnerPhonenumber);
		
		txtOwnerPhonenumber = new JTextField();
		txtOwnerPhonenumber.setBounds(140, 65, 200, 20);
		getContentPane().add(txtOwnerPhonenumber);
		txtOwnerPhonenumber.setColumns(10);
		
		JButton btnCreate = new JButton("Create");
		btnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				createOwner();
			}
		});
		btnCreate.setBounds(254, 96, 86, 20);
		getContentPane().add(btnCreate);
		setVisible(true);
	}
	
	public void createOwner() {
		boolean succes = false;
		String ownerName = txtOwnerName.getText();
		String address = txtOwnerAddress.getText();
		String phoneNumber = txtOwnerPhonenumber.getText();
		
		if(ownerName.isEmpty() != true && address.isEmpty() != true && phoneNumber.isEmpty() != true) {
		succes = oCtr.createOwner(ownerName, address, phoneNumber);
		close();
		ownerPanel.updatePanel();
		} else {
			JOptionPane.showMessageDialog(getContentPane(), "You have to fill in all textFields", "Error Message", JOptionPane.ERROR_MESSAGE);
		}
		
	}
	public void close() {
		setVisible(false);
		dispose();
	}
} 
